const path = require("path");

const {
  create,
  findAllProduct,
  delProduct,
  findAProduct,
  updateById,
} = require("../../services/admin/product.service");
const {
  unSupportedFileType,
  invalidProductID,
} = require("../../constant/err.type");

class ProductController {
  //   async upload(ctx, next) {
  //     //files后边跟.文件名
  //     // console.log(ctx.request.files.file);
  //     const { file } = ctx.request.files;
  //     const fileTypes = ["image/jepg", "image/png"];
  //     if (file) {
  //       if (!fileTypes.includes(file.mimetype)) {
  //         return ctx.app.emit("error", unSupportedFileType, ctx);
  //       }
  //       ctx.body = {
  //         code: 0,
  //         message: "图片上传成功",
  //         result: {
  //           goods_img: path.basename(file.filepath), //拿到文件名
  //         },
  //       };
  //       // await next();
  //     } else {
  //       return ctx.app.emit("error", fileUploadError, ctx);
  //     }
  //   }
  async add(ctx) {
    //files后边跟.文件名
    // console.log(ctx.request.files.file);

    try {
      //拿到要创建的数据
      const { title, introduction, detail } = ctx.request.body;
      let cover = "";
      const { file } = ctx.request.files;
      const fileTypes = ["image/jpeg", "image/png", "image/jpg"];
      if (file) {
        if (!fileTypes.includes(file.mimetype)) {
          return ctx.app.emit("error", unSupportedFileType, ctx);
        }
        //public 作为静态资源目录 直接用其相对位置
        cover = `/productuploads/${path.basename(file.filepath)}`;
      }
      const res = await create({
        title,
        introduction,
        detail,
        cover,
        editTime: new Date(),
      });
      if (res && cover) {
        ctx.body = {
          code: 0,
          message: "添加产品成功",
          result: {
            title,
            introduction,
            detail,
            cover,
            editTime: new Date(),
          },
        };
      } else {
        ctx.body = {
          code: 1,
          message: "添加产品失败",
        };
      }
    } catch (error) {
      console.error("添加产品过程中出现错误", error);
      ctx.body = {
        code: 1,
        message: "添加产品失败",
      };
    }
  }
  async findAll(ctx) {
    //1.解析pageNum和pageSize
    const { pageNum = 1, pageSize = 10 } = ctx.request.query;
    //2.调用数据处理的相关方法
    const res = await findAllProduct(pageNum, pageSize);
    //3.返回结果
    console.log(res);

    ctx.body = {
      code: 0,
      message: "获取产品列表成功",
      result: res,
    };
  }

  async delList(ctx) {
    try {
      const res = await delProduct(ctx.params.id);

      if (res !== null) {
        ctx.body = {
          code: 0,
          message: "产品删除成功",
          result: res,
        };
      } else {
        return ctx.app.emit("error", invalidProductID, ctx);
      }
    } catch (error) {
      console.error(error);
    }
  }

  async findOne(ctx) {
    const res = await findAProduct({ id: ctx.params.id });
    if (res) {
      ctx.body = {
        code: 0,
        message: "获取新闻信息成功",
        result: res,
      };
    }
  }

  async updateList(ctx) {
    try {
      //拿到要修改的数据
      const { title, introduction, detail, id } = ctx.request.body;
      let cover = "";
      const { file } = ctx.request.files;
      const fileTypes = ["image/jpeg", "image/png", "image/jpg"];
      if (file) {
        if (!fileTypes.includes(file.mimetype)) {
          return ctx.app.emit("error", unSupportedFileType, ctx);
        }
        //public 作为静态资源目录 直接用其相对位置
        cover = `/productuploads/${path.basename(file.filepath)}`;
      }
      const res = await updateById({
        id,
        title,
        introduction,
        detail,
        cover,
        editTime: new Date(),
      });
      if (res && cover) {
        ctx.body = {
          code: 0,
          message: "更新产品成功",
          result: {
            title,
            introduction,
            detail,
            cover,
            editTime: new Date(),
          },
        };
      } else {
        ctx.body = {
          code: 1,
          message: "更新产品失败",
        };
      }
    } catch (error) {
      console.error("更新产品过程中出现错误", error);
      ctx.body = {
        code: 1,
        message: "更新产品失败",
      };
    }
  }
}

module.exports = new ProductController();
